package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.Grade;
import com.jstyle.jclifexd.model.Run3;
import com.jstyle.jclifexd.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecordAdapter extends RecyclerView.Adapter  {
    Context context;
    Grade mDataList;

    boolean check3=false;
    boolean check5=false;
    boolean check10=false;
    boolean check15=false;
    boolean check22=false;
    boolean check50=false;
    public RecordAdapter(Context mycontext) {
        this.context=mycontext;
    }

    public void updateData(Grade list){
        this.mDataList=list;
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_record, parent, false);
        RankViewHolder rankViewHolder=new RankViewHolder(view);
        return rankViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if(null==mDataList){return;}
       final RankViewHolder viewholder=(RankViewHolder)viewHolder;
        switch (i){
            case 0:
                viewholder.recordimgvalue.setImageDrawable(context.getResources().getDrawable(R.drawable.record_3k));
                viewholder.recordvalue.setText(context.getResources().getString(R.string.kilometer3));
                if(null!=mDataList.getRun3()&&mDataList.getRun3().size()>0&&!mDataList.getRun3().equals("[]")){
                    viewholder.record_time.setVisibility(View.VISIBLE);
                    viewholder.imgup.setVisibility(View.VISIBLE);
                    viewholder.record_time.setText(DateUtil.getSecond(mDataList.getRun3().get(0).getActiveTime()));
                    RecorderAdapter rr=new RecorderAdapter(context);
                    viewholder.rc_eecyclerView.setLayoutManager(new LinearLayoutManager(context));
                    viewholder.rc_eecyclerView.setAdapter(rr);
                    rr.updateData(mDataList.getRun3());
                    if(viewholder.imgup.getVisibility()==View.VISIBLE){
                    viewholder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(!check3){
                                check3=true;
                                StartAnimation(viewholder.imgup,0);
                                viewholder.rc_eecyclerView.setVisibility(View.VISIBLE);
                            }else{
                                check3=false;
                                StartAnimation(viewholder.imgup,1);
                                viewholder.rc_eecyclerView.setVisibility(View.GONE);
                            }
                        }
                    });}

                }else{
                    viewholder.record_time.setVisibility(View.GONE);
                    viewholder.imgup.setVisibility(View.GONE);
                }
                break;
            case 1:
                viewholder.recordimgvalue.setImageDrawable(context.getResources().getDrawable(R.drawable.record_5k));
                viewholder.recordvalue.setText(context.getResources().getString(R.string.kilometer5));
                if(null!=mDataList.getRun5()&&mDataList.getRun5().size()>0&&!mDataList.getRun5().equals("[]")){
                    viewholder.record_time.setVisibility(View.VISIBLE);
                    viewholder.imgup.setVisibility(View.VISIBLE);
                    viewholder.record_time.setText(DateUtil.getSecond(mDataList.getRun5().get(0).getActiveTime()));
                    RecorderAdapter rr=new RecorderAdapter(context);
                    viewholder.rc_eecyclerView.setLayoutManager(new LinearLayoutManager(context));
                    viewholder.rc_eecyclerView.setAdapter(rr);

                    List<Run3> run3=new ArrayList<>();
                    for (int ff=0;ff<mDataList.getRun5().size();ff++){
                        Run3 run3value=new Run3();
                        run3value.setActiveTime(mDataList.getRun5().get(ff).getActiveTime());
                        run3value.setDay(mDataList.getRun5().get(ff).getDay());
                        run3value.setDistance(mDataList.getRun5().get(ff).getDistance());
                        run3.add(run3value);
                    }
                    rr.updateData(run3);
                    if(viewholder.imgup.getVisibility()==View.VISIBLE){
                    viewholder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(!check5){
                                check5=true;
                                StartAnimation(viewholder.imgup,0);
                                viewholder.rc_eecyclerView.setVisibility(View.VISIBLE);
                            }else{
                                check5=false;
                                StartAnimation(viewholder.imgup,1);
                                viewholder.rc_eecyclerView.setVisibility(View.GONE);
                            }
                        }
                    });}
                }else{
                    viewholder.record_time.setVisibility(View.GONE);
                    viewholder.imgup.setVisibility(View.GONE);
                }
                break;
            case 2:
                viewholder.recordimgvalue.setImageDrawable(context.getResources().getDrawable(R.drawable.record_10k));
                viewholder.recordvalue.setText(context.getResources().getString(R.string.kilometer10));
                if(null!=mDataList.getRun10()&&mDataList.getRun10().size()>0&&!mDataList.getRun10().equals("[]")){
                    viewholder.record_time.setVisibility(View.VISIBLE);
                    viewholder.imgup.setVisibility(View.VISIBLE);
                    viewholder.record_time.setText(DateUtil.getSecond(mDataList.getRun10().get(0).getActiveTime()));
                    RecorderAdapter rr=new RecorderAdapter(context);
                    viewholder.rc_eecyclerView.setLayoutManager(new LinearLayoutManager(context));
                    viewholder.rc_eecyclerView.setAdapter(rr);
                    List<Run3> run3=new ArrayList<>();
                    for (int ff=0;ff<mDataList.getRun10().size();ff++){
                        Run3 run3value=new Run3();
                        run3value.setActiveTime(mDataList.getRun10().get(ff).getActiveTime());
                        run3value.setDay(mDataList.getRun10().get(ff).getDay());
                        run3value.setDistance(mDataList.getRun10().get(ff).getDistance());
                        run3.add(run3value);
                    }
                    rr.updateData(run3);
                    if(viewholder.imgup.getVisibility()==View.VISIBLE){
                    viewholder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(!check10){
                                check10=true;
                                StartAnimation(viewholder.imgup,0);
                                viewholder.rc_eecyclerView.setVisibility(View.VISIBLE);
                            }else{
                                check10=false;
                                StartAnimation(viewholder.imgup,1);
                                viewholder.rc_eecyclerView.setVisibility(View.GONE);
                            }
                        }
                    });}
                }else{
                    viewholder.record_time.setVisibility(View.GONE);
                    viewholder.imgup.setVisibility(View.GONE);
                }
                break;
            case 3:
                viewholder.recordimgvalue.setImageDrawable(context.getResources().getDrawable(R.drawable.record_half));
                viewholder.recordvalue.setText(context.getResources().getString(R.string.kilometer22));
                if(null!=mDataList.getRun22()&&mDataList.getRun22().size()>0&&!mDataList.getRun22().equals("[]")){
                    viewholder.record_time.setVisibility(View.VISIBLE);
                    viewholder.imgup.setVisibility(View.VISIBLE);
                    viewholder.record_time.setText(DateUtil.getSecond(mDataList.getRun22().get(0).getActiveTime()));
                    RecorderAdapter rr=new RecorderAdapter(context);
                    viewholder.rc_eecyclerView.setLayoutManager(new LinearLayoutManager(context));
                    viewholder.rc_eecyclerView.setAdapter(rr);
                    List<Run3> run3=new ArrayList<>();
                    for (int ff=0;ff<mDataList.getRun22().size();ff++){
                        Run3 run3value=new Run3();
                        run3value.setActiveTime(mDataList.getRun22().get(ff).getActiveTime());
                        run3value.setDay(mDataList.getRun22().get(ff).getDay());
                        run3value.setDistance(mDataList.getRun22().get(ff).getDistance());
                        run3.add(run3value);
                    }
                    rr.updateData(run3);
                    if(viewholder.imgup.getVisibility()==View.VISIBLE){
                    viewholder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(!check15){
                                check15=true;
                                StartAnimation(viewholder.imgup,0);
                                viewholder.rc_eecyclerView.setVisibility(View.VISIBLE);
                            }else{
                                check15=false;
                                StartAnimation(viewholder.imgup,1);
                                viewholder.rc_eecyclerView.setVisibility(View.GONE);
                            }
                        }
                    });}

                }else{
                    viewholder.record_time.setVisibility(View.GONE);
                    viewholder.imgup.setVisibility(View.GONE);
                }
                break;
            case 4:
                viewholder.recordimgvalue.setImageDrawable(context.getResources().getDrawable(R.drawable.record_full));
                viewholder.recordvalue.setText(context.getResources().getString(R.string.kilometer42));
                if(null!=mDataList.getRun42()&&mDataList.getRun42().size()>0&&!mDataList.getRun42().equals("[]")){
                    viewholder.record_time.setVisibility(View.VISIBLE);
                    viewholder.imgup.setVisibility(View.VISIBLE);
                    viewholder.record_time.setText(DateUtil.getSecond(mDataList.getRun42().get(0).getActiveTime()));

                    RecorderAdapter rr=new RecorderAdapter(context);
                    viewholder.rc_eecyclerView.setLayoutManager(new LinearLayoutManager(context));
                    viewholder.rc_eecyclerView.setAdapter(rr);
                    List<Run3> run3=new ArrayList<>();
                    for (int ff=0;ff<mDataList.getRun42().size();ff++){
                        Run3 run3value=new Run3();
                        run3value.setActiveTime(mDataList.getRun42().get(ff).getActiveTime());
                        run3value.setDay(mDataList.getRun42().get(ff).getDay());
                        run3value.setDistance(mDataList.getRun42().get(ff).getDistance());
                        run3.add(run3value);
                    }
                    rr.updateData(run3);
                    if(viewholder.imgup.getVisibility()==View.VISIBLE) {
                        if (viewholder.rc_eecyclerView.getVisibility() == View.VISIBLE) {
                            viewholder.itemView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (!check22) {
                                        check22 = true;
                                        StartAnimation(viewholder.imgup, 0);
                                        viewholder.rc_eecyclerView.setVisibility(View.VISIBLE);
                                    } else {
                                        check22 = false;
                                        StartAnimation(viewholder.imgup, 1);
                                        viewholder.rc_eecyclerView.setVisibility(View.GONE);
                                    }
                                }
                            });
                        }
                    }
                }else{
                    viewholder.record_time.setVisibility(View.GONE);
                    viewholder.imgup.setVisibility(View.GONE);
                }
                break;
            case 5:
                viewholder.recordimgvalue.setImageDrawable(context.getResources().getDrawable(R.drawable.record_50k));
                viewholder.recordvalue.setText(context.getResources().getString(R.string.cycling50));
                if(null!=mDataList.getCyc50()&&mDataList.getCyc50().size()>0&&!mDataList.getCyc50().equals("[]")){
                    viewholder.record_time.setVisibility(View.VISIBLE);
                    viewholder.imgup.setVisibility(View.VISIBLE);
                    viewholder.record_time.setText(DateUtil.getSecond(mDataList.getCyc50().get(0).getActiveTime()));

                    RecorderAdapter rr=new RecorderAdapter(context);
                    viewholder.rc_eecyclerView.setLayoutManager(new LinearLayoutManager(context));
                    viewholder.rc_eecyclerView.setAdapter(rr);
                    List<Run3> run3=new ArrayList<>();
                    for (int ff=0;ff<mDataList.getCyc50().size();ff++){
                        Run3 run3value=new Run3();
                        run3value.setActiveTime(mDataList.getCyc50().get(ff).getActiveTime());
                        run3value.setDay(mDataList.getCyc50().get(ff).getDay());
                        run3value.setDistance(mDataList.getCyc50().get(ff).getDistance());
                        run3.add(run3value);
                    }
                    rr.updateData(run3);
                    if(viewholder.imgup.getVisibility()==View.VISIBLE){
                    viewholder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(!check50){
                                check50=true;
                                StartAnimation(viewholder.imgup,0);
                                viewholder.rc_eecyclerView.setVisibility(View.VISIBLE);
                            }else{
                                check50=false;
                                StartAnimation(viewholder.imgup,1);
                                viewholder.rc_eecyclerView.setVisibility(View.GONE);
                            }
                        }
                    });}
                }else{
                    viewholder.record_time.setVisibility(View.GONE);
                    viewholder.imgup.setVisibility(View.GONE);
                }
                break;
        }



    }


    private void StartAnimation(ImageView view,int type){
       if(0==type){
            Animation   anim =new RotateAnimation(0f, 180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setFillAfter(true); // 设置保持动画最后的状态
            anim.setDuration(500); // 设置动画时间
            anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
            view.startAnimation(anim);
        }else{
            Animation    anim =new RotateAnimation(180f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            anim.setFillAfter(true); // 设置保持动画最后的状态
            anim.setDuration(500); // 设置动画时间
            anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
            view.startAnimation(anim);
        }


    }
    @Override
    public int getItemCount() {
        return null==mDataList?0:6;
    }




    class RankViewHolder extends BaseViewHolder{
        @BindView(R.id.recordimgvalue)
        ImageView recordimgvalue;
        @BindView(R.id.imgup)
        ImageView imgup;
        @BindView(R.id.recordvalue)
        TextView recordvalue;
        @BindView(R.id.record_time)
        TextView record_time;
        @BindView(R.id.rc_eecyclerView)
        RecyclerView rc_eecyclerView;

         RankViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
